package com.xjscrm.console.web.circle;


import cn.hutool.core.util.IdUtil;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.common.util.LoginContext;
import com.xjscrm.console.dto.circle.CircleTaskGroupDTO;
import com.xjscrm.console.dto.circle.CircleTaskGroupSortDTO;
import com.xjscrm.console.entity.CircleTaskGroup;
import com.xjscrm.console.service.customer.circle.CircleTaskGroupService;
import com.xjscrm.console.vo.CircleTaskGroupVO;
import com.xjscrm.console.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 朋友圈任务分组(CircleTaskGroup)表控制层
 *
 * @author szl
 * @since 2024-04-09 10:27:53
 */
@RestController
@RequestMapping("/circleTaskGroup")
@Api(tags = "朋友圈任务分组控制器")
public class CircleTaskGroupController {
    /**
     * 服务对象
     */
    @Autowired
    private CircleTaskGroupService circleTaskGroupService;

    /**
     * 分页查询所有数据
     *
     * @param source 查询实体
     * @return 所有数据
     */
    @PostMapping("/search")
    @ApiOperation(value = "朋友圈任务分组分页查询", response = CircleTaskGroupVO.class)
    public Object search(@RequestBody CircleTaskGroupDTO source) {
        LoginVO login = LoginContext.getLogin();
        return circleTaskGroupService.listByStaffId(login.getMerId(), login.getId(), source.getName());
    }

    /**
     * 新增数据
     *
     * @param source 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation("新增朋友圈任务分组")
    public Object save(@RequestBody CircleTaskGroupDTO source) {
        LoginVO login = LoginContext.getLogin();

        CircleTaskGroup target = new CircleTaskGroup();

        Integer maxSort = circleTaskGroupService.selectMaxSort(login.getMerId());

        target.setId(IdUtil.getSnowflakeNextId());
        target.setMerId(login.getMerId());
        target.setName(source.getName());
        target.setCreateBy(login.getId());
        target.setCreateTime((int) (System.currentTimeMillis() / 1000L));
        target.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
        target.setSort(ObjectUtils.defaultIfNull(maxSort, 0) + 1);
        return this.circleTaskGroupService.insertSelective(target);
    }

    /**
     * 修改数据
     *
     * @param source 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation("修改朋友圈任务分组")
    public Object update(@RequestBody CircleTaskGroupDTO source) {
        if (Objects.isNull(source) || Objects.isNull(source.getId())) {
            throw new BusinessException("修改分组参数错误");
        }
        LoginVO login = LoginContext.getLogin();
        CircleTaskGroup target = new CircleTaskGroup();

        target.setMerId(login.getMerId());
        target.setId(source.getId());
        target.setName(source.getName());
        target.setUpdateTime((int) (System.currentTimeMillis() / 1000L));

        return this.circleTaskGroupService.updateByPrimaryKeySelective(target);
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @PostMapping("/del")
    @ApiOperation("删除朋友圈任务分组")
    public Object delete(@RequestBody CircleTaskGroupDTO source) {
        LoginVO login = LoginContext.getLogin();

        return this.circleTaskGroupService.deleteByPrimaryKey(login.getMerId(), source.getId());
    }

    @PostMapping("/sort")
    @ApiOperation("排序朋友圈任务分组")
    public Object sort(@RequestBody CircleTaskGroupSortDTO source) {
        if (Objects.isNull(source) || ObjectUtils.isEmpty(source.getSortItems())) {
            throw new BusinessException("排序分组参数错误");
        }
        LoginVO login = LoginContext.getLogin();

        for (CircleTaskGroupSortDTO.SortItem sortItem : source.getSortItems()) {
            CircleTaskGroup target = new CircleTaskGroup();

            target.setMerId(login.getMerId());
            target.setId(sortItem.getId());
            target.setSort(sortItem.getSort());
            target.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
            this.circleTaskGroupService.updateByPrimaryKeySelective(target);
        }
        return true;
    }
}

